using System.Collections.Generic;

//
// * Copyright (c) 2002-2009 "Neo Technology,"
// *     Network Engine for Objects in Lund AB [http://neotechnology.com]
// *
// * This file is part of Neo4j.
// * 
// * Neo4j is free software: you can redistribute it and/or modify
// * it under the terms of the GNU Affero General Public License as
// * published by the Free Software Foundation, either version 3 of the
// * License, or (at your option) any later version.
// * 
// * This program is distributed in the hope that it will be useful,
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// * GNU Affero General Public License for more details.
// * 
// * You should have received a copy of the GNU Affero General Public License
// * along with this program. If not, see <http://www.gnu.org/licenses/>.
// 
namespace org.neo4j.kernel.impl.transaction
{


	using Test = junit.framework.Test;
	using TestCase = junit.framework.TestCase;
	using TestSuite = junit.framework.TestSuite;

	using TxLog = org.neo4j.kernel.impl.transaction.TxLog;
	using Record = org.neo4j.kernel.impl.transaction.TxLog.Record;

	public class TestTxLog : TestCase
	{
		public TestTxLog(string name) : base(name)
		{
		}

		public static Test suite()
		{
			return new TestSuite(typeof(TestTxLog));
		}

		private void assertEqualByteArray(sbyte[] a, sbyte[] b)
		{
			assertTrue(a.Length == b.Length);
			for (int i = 0; i < a.Length; i++)
			{
				assertEquals(a[i], b[i]);
			}
		}

//JAVA TO VB & C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public void testTxLog() throws IOException
		public virtual void testTxLog()
		{
			File file = new File("tx_test_log.tx");
			if (file.exists())
			{
				file.delete();
			}
			try
			{
				TxLog txLog = new TxLog("tx_test_log.tx");
				assertTrue(!txLog.getDanglingRecords().MoveNext());
				sbyte[] globalId = new sbyte[64];
				sbyte[] branchId = new sbyte[45];
				txLog.txStart(globalId);
				txLog.addBranch(globalId, branchId);
				assertEquals(2, txLog.getRecordCount());
			// List lists[] = txLog.getDanglingRecords();
//JAVA TO VB & C# CONVERTER TODO TASK: Java wildcard generics are not converted to .NET:
//ORIGINAL LINE: List<?> lists[] = getRecordLists(txLog.getDanglingRecords());
				List<?>[] lists = getRecordLists(txLog.getDanglingRecords());
				assertEquals(1, lists.Length);
//JAVA TO VB & C# CONVERTER TODO TASK: Java wildcard generics are not converted to .NET:
//ORIGINAL LINE: List<?> records = lists[0];
				List<?> records = lists[0];
				assertEquals(2, records.Count);
				TxLog.Record record = (TxLog.Record) records[0];
				assertEquals(TxLog.TX_START, record.getType());
				assertEqualByteArray(globalId, record.getGlobalId());
				assertTrue(null == record.getBranchId());
				record = (TxLog.Record) records[1];
				assertEquals(TxLog.BRANCH_ADD, record.getType());
				assertEqualByteArray(globalId, record.getGlobalId());
				assertEqualByteArray(branchId, record.getBranchId());
				txLog.markAsCommitting(globalId);
				assertEquals(3, txLog.getRecordCount());
				txLog.Close();
				txLog = new TxLog("tx_test_log.tx");
				assertEquals(0, txLog.getRecordCount());
				lists = getRecordLists(txLog.getDanglingRecords());
				assertEquals(1, lists.Length);
				records = lists[0];
				assertEquals(3, records.Count);
				record = (TxLog.Record) records[0];
				assertEquals(TxLog.TX_START, record.getType());
				assertEqualByteArray(globalId, record.getGlobalId());
				assertTrue(null == record.getBranchId());
				record = (TxLog.Record) records[1];
				assertEquals(TxLog.BRANCH_ADD, record.getType());
				assertEqualByteArray(globalId, record.getGlobalId());
				assertEqualByteArray(branchId, record.getBranchId());
				record = (TxLog.Record) records[2];
				assertEquals(TxLog.MARK_COMMIT, record.getType());
				assertEqualByteArray(globalId, record.getGlobalId());
				assertTrue(null == record.getBranchId());
				txLog.txDone(globalId);
				assertEquals(1, txLog.getRecordCount());
				assertEquals(0, getRecordLists(txLog.getDanglingRecords()).length);
				txLog.Close();
				txLog = new TxLog("tx_test_log.tx");
				assertEquals(0, getRecordLists(txLog.getDanglingRecords()).length);
				txLog.Close();
			}
			finally
			{
				file = new File("tx_test_log.tx");
				if (file.exists())
				{
					file.delete();
				}
			}
		}

//JAVA TO VB & C# CONVERTER TODO TASK: Java wildcard generics are not converted to .NET:
//ORIGINAL LINE: private List<?>[] getRecordLists(Iterator<List<Record>> danglingRecords)
		private List<?>[] getRecordLists(Iterator<List<Record>> danglingRecords)
		{
//JAVA TO VB & C# CONVERTER TODO TASK: Java wildcard generics are not converted to .NET:
//ORIGINAL LINE: List<List<?>> list = new ArrayList<List<?>>();
			List<List<?>> list = new List<List<?>>();
			while (danglingRecords.MoveNext())
			{
				list.Add(danglingRecords.Current);
			}
			return list.ToArray(new List[list.Count]);
		}

//JAVA TO VB & C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public void testTruncateTxLog() throws IOException
		public virtual void testTruncateTxLog()
		{
			File file = new File("tx_test_log.tx");
			if (file.exists())
			{
				file.delete();
			}
			try
			{
				TxLog txLog = new TxLog("tx_test_log.tx");
				sbyte[] globalId = new sbyte[64];
				sbyte[] branchId = new sbyte[45];
				txLog.txStart(globalId);
				txLog.addBranch(globalId, branchId);
				txLog.markAsCommitting(globalId);
				txLog.truncate();
				assertEquals(0, getRecordLists(txLog.getDanglingRecords()).length);
				txLog.Close();
				txLog = new TxLog("tx_test_log.tx");
				txLog.txStart(globalId);
				txLog.addBranch(globalId, branchId);
				txLog.markAsCommitting(globalId);
				txLog.Close();
				txLog = new TxLog("tx_test_log.tx");
				assertEquals(1, getRecordLists(txLog.getDanglingRecords()).length);
				txLog.truncate();
				assertEquals(0, getRecordLists(txLog.getDanglingRecords()).length);
			}
			finally
			{
				file = new File("tx_test_log.tx");
				if (file.exists())
				{
					file.delete();
				}
			}
		}

		public virtual void testTxRecovery()
		{

		}
	}
}